using System.Collections.ObjectModel;
using System.Linq;
using memdb_demo.Models;

namespace memdb_demo.ViewModels
{
    public partial class OrderMonitorViewModel : ViewModelBase
    {
        private uint _offset=0;
        public uint Offset { get=>_offset;set=>base.RaiseAndSetIfChanged(ref _offset,value);}

        private uint _size=0;
        public uint Size { get=>_size;set=>base.RaiseAndSetIfChanged(ref _size,value);}

        private ObservableCollection<MonitorOrder> _items=new ObservableCollection<MonitorOrder>();
        public ObservableCollection<MonitorOrder> Items { get=>_items;set=>base.RaiseAndSetIfChanged(ref _items,value);}

        private string _errorMessage=string.Empty;
        public string ErrorMessage { get=>_errorMessage;set=>base.RaiseAndSetIfChanged(ref _errorMessage,value);}

        
        public OrderMonitorModel Model {get;set;} = new OrderMonitorModel();


        public OrderMonitorViewModel(){
            Refresh();
        }


        public void Refresh(){
            Model.Offset = this.Offset;
            Model.Size = this.Size;
            var result = Model.Query();
            if (result.IsSuccess){
                this.Items.Clear();
                foreach(var record in result.Data){
                    this.Items.Add(new MonitorOrder(record));
                }
            } else {
                //MESSAGEBOX.SHOW
                this.ErrorMessage = result.ErrorMessage;
            }
        }

    }
}

